/*
 * ${tableModel.domainName}ServiceImpl.java  Created On ${tableModel.currentDate}
 * Copyright(c) 2015 Treebear Inc.
 * ALL Rights Reserved.
 */
package ${packname}.controller;

import ${packname}.entity.${tableModel.domainName};
import ${packname}.service.${tableModel.domainName}Service;
import com.fsnip.common.tools.base.entity.PageList;
import com.fsnip.common.tools.base.controller.BaseController;
import com.fsnip.common.tools.jdbc.QueryBuilder;
import com.fsnip.common.tools.utils.VoFieldToColumnName;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
/**
 * 类${tableModel.domainName}ServiceImpl的实现描述：${tableModel.tableChineseName}
 * 
 * @author ${author} ${tableModel.currentDate}
 * @data ${tableModel.currentTime}
 */
@Controller
@RequestMapping(value = "/${tableModel.tableName}")
public class ${tableModel.domainName}Controller extends BaseController{
	private Log LOG = LogFactory.getLog(${tableModel.domainName}.class);

	@Autowired(required=true)
	private ${tableModel.domainName}Service service;

	public Map<Object, Object> modelMap;// 定义返回的对象

	/**
	 * 新增${tableModel.tableChineseName}信息
	 * 
	 * @param session
	 * @param request
	 * @param response
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "add",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation("新增${tableModel.tableChineseName}信息")
	public  Object add(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,${tableModel.domainName} entity){
		modelMap = new HashMap<Object, Object>();
		entity = service.add(entity);
		modelMap.put(RESULT_FLAG, SUCCESS_FLAG);
		modelMap.put(RESULT_OBJECT, entity);
		return modelMap;
	}

	/**
	 * 单条数据删除${tableModel.tableChineseName}信息
	 * 
	 * @param session
	 * @param request
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "deleteById",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation("删除${tableModel.tableChineseName}信息")
	public  Object deleteById(HttpSession session, HttpServletRequest request,
			${tableModel.domainName} entity) {
		modelMap = new HashMap<Object, Object>();// 定义返回的对象
		service.remove(entity);
		modelMap.put("ret", "0");
		return modelMap;
	}
	
	/**
	 * 修改${tableModel.tableChineseName}信息
	 * 
	 * @param session
	 * @param request
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "update",method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation("更新${tableModel.tableChineseName}信息")
	public  Object update(HttpSession session, HttpServletRequest request,
			${tableModel.domainName} entity) {
		modelMap = new HashMap<Object, Object>();// 定义返回的对象
		service.update(entity);
		modelMap.put(RESULT_FLAG, SUCCESS_FLAG);
		modelMap.put(RESULT_OBJECT, entity);
		return modelMap;
	}
	
	/**
	 * 修改或删除查询，${tableModel.tableChineseName}信息
	 * 
	 * @param session
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "findById",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation("修改/查询${tableModel.tableChineseName}信息")
	<#list tableModel.columnList as columnModel>
	<#if columnModel.isPK == true>
	public  Object findById(HttpSession session, HttpServletRequest request,
			${columnModel.columnType} id) {
	</#if>
	</#list>
		${tableModel.domainName} entity = service.findByPrimaryKey(id);
		modelMap = new HashMap<Object, Object>();// 定义返回的对象
		modelMap.put(RESULT_FLAG, SUCCESS_FLAG);
		modelMap.put(RESULT_OBJECT, entity);
		return modelMap;
		
	}

	/**
	 * 条件查询${tableModel.tableChineseName}信息
	 * 
	 * @param session
	 * @param request
	 * @param pageList
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "simpleQuery",method = RequestMethod.GET)
	@ApiOperation("条件查询${tableModel.tableChineseName}信息")
	@ResponseBody
	public Object simpleQuery(HttpSession session, HttpServletRequest request,PageList<${tableModel.domainName}> pageList,
							@RequestParam(defaultValue = "10") int rows,@RequestParam(defaultValue = "1")int page, 
							${tableModel.domainName} entity,String order, String sort) {
		modelMap = new HashMap<Object, Object>();// 定义返回的对象
		pageList.setCondtitonStr(getQueryCondition(request));
		pageList.setPageSize(rows);
		pageList.setCurrentPage(page);
		if (order != null) {
			pageList.setOrderType(order);
		}
		if (sort != null) {
			pageList.setOrderField(VoFieldToColumnName.getInstance()
					.convertToColumnField(sort));
		}
		pageList = service.getByCondition(pageList);
		modelMap.put(RESULT_ROWS, pageList.getItems());
		modelMap.put(RESULT_TOTAL, pageList.getTotalCount());
		return modelMap;
	}
	
	/**
	 * 从request获得查询条件并拼装成查询的SQL语句
	 * 
	 * @param qca
	 * @return 查询的SQL语句
	 */
	private String getQueryCondition(final HttpServletRequest qca) {
		if ("true".equals(qca.getParameter("backFlag"))) {
			return "";
		} else {
			return new QueryBuilder() {
				public void andCondition() {
					conditions = new String[] {
						<#list tableModel.columnList as columnModel>
						<#if "${columnModel.columnType}" == "Date" || "${columnModel.columnType}" == "Timestamp">
						pushCondition(qca, "${columnModel.columnName2Camel}Start",">=to_date('", "','YYYY-MM-DD')","${columnModel.columnName}"),
						pushConditionEndDate(qca, "${columnModel.columnName2Camel}End","< to_date('", "','YYYY-MM-DD')","${columnModel.columnName}"),
						<#elseif "${columnModel.columnType}" == "Double" || "${columnModel.columnType}" == "Integer">
						pushCondition(qca, "${columnModel.columnName2Camel}", "='", "'","${columnModel.columnName}"),
						<#else>	
						pushCondition(qca, "${columnModel.columnName2Camel}", "like '", "%'","${columnModel.columnName}"),
						</#if>
						</#list>
					};
				}
			}.build();
		}
	}
}